package cn.edu.zime.tjh47;

import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

public class Class10 extends AppCompatActivity {
    private TextView tv_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.class10);
        ActionBar bar = getSupportActionBar();
        bar.setTitle("登录");
        bar.setIcon(R.drawable.as_logo);
        bar.setDisplayShowHomeEnabled(true);
        bar.setDisplayHomeAsUpEnabled(true);
        tv_info = findViewById(R.id.tv_info);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.class10_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.login) {
            showDlgLogin();
            return true;
        } else if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void showDlgLogin() {
        LayoutInflater layoutInflater = getLayoutInflater();
        final View view = layoutInflater.inflate(R.layout.class10_login, null);
        read(view);
        new AlertDialog.Builder(this)
                .setTitle("登录")
                .setView(view)
                .setPositiveButton("确定", (dialogInterface, i) -> {
                    EditText et_User = view.findViewById(R.id.et_account);
                    EditText et_Pwd = view.findViewById(R.id.et_password);
                    CheckBox checkBox = view.findViewById(R.id.cb_remember);
                    String user = et_User.getText().toString();
                    String pwd = et_Pwd.getText().toString();
                    if (user.equals("tongjiahao") && pwd.equals("123456")) {
                        if (checkBox.isChecked()) {
                            save(user, pwd);
                        }
                        String info = String.format("欢迎你%s", user);
                        tv_info.setText(info);
                    } else {
                        Toast.makeText(Class10.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        save("", "");
                        tv_info.setText(" ");
                    }
                })
                .create()
                .show();
    }

    private void save(String username, String password) {
        SharedPreferences sharedPreferences = getSharedPreferences("Ricardo", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.commit();
    }

    private void read(View view) {
        SharedPreferences sharedPreferences = getSharedPreferences("Ricardo", MODE_PRIVATE);
        EditText et_User = view.findViewById(R.id.et_account);
        EditText et_Pwd = view.findViewById(R.id.et_password);
        String user = sharedPreferences.getString("username", "");
        String pwd = sharedPreferences.getString("password", "");
        et_User.setText(user);
        et_Pwd.setText(pwd);
    }

    public void back(View view) {
        finish();
    }
}